$(function () {
    container.off("click");
    function getClassroomsByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "classroomName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getClassroomsByName(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getClassroomsByName(0);
    });

    // 获取添加实验室的界面
    $("#addClassroom").click(function() {
        $("#classroomDialogTitle").html("添加实验室");
        axios({
            method:'get',
            url: "/classroom/add"
        }).then(function (response) {
            $("#classroomForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取编辑实验室界面
    container.on("click","#edit-classroom",function () {
        $("#classroomDialogTitle").html("编辑实验室");
        axios({
            method:'get',
            url:"/classroom/edit/"+$(this).attr('classroomId')
        }).then(function (response) {
            $("#classroomForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //提交操作，清空表单
    $("#classroomForm").submit(function() {
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:"POST",
            url:"/classroom",
            data:$('#classroomForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#classroomForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getClassroomsByName(0);
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });

        $("#classroomDialogClose").click();
        return false;
    });


    //删除实验室
    container.on("click","#delete-classroom",function () {
       // 获取 CSRF Token
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:'DELETE',
            url:"/classroom/"+$(this).attr('classroomId'),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            if (response.data.success) {
                // 从新刷新主界面
                getClassroomsByName(0);
                toastr.success(response.data.message);
            } else {
                toastr.error(response.data.message);
            }
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //实验室课表
    container.on("click","#classroom-timetables",function () {
        $("#classroomDialogTitle").html("实验室课表");
        axios({
            method:'GET',
            url:"/classroom/"+$(this).attr('classroomId')+"/timetables",
        }).then(function (response) {
            $('.modal-dialog').addClass("modal-lg");
            $("#classroomForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //关闭Dialog界面
    $("#classroomDialogClose").click(function () {
        $('.modal-dialog').removeClass("modal-lg");
        $("#classroomForm").html("");
    });

});